package com.supermap.qdsearch.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.supermap.qdsearch.activity.FileManageActivity;
import com.supermap.qdsearch.R;
import com.supermap.qdsearch.utils.SPUtils;
import com.supermap.qdsearch.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends Fragment {


    Unbinder unbinder;
    @BindView(R.id.tv_img_path)
    EditText tvImgPath;
    @BindView(R.id.tv_server_path)
    EditText tvServerPath;
    @BindView(R.id.tv_pic_delete)
    Button tvPicDelete;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_release_date)
    TextView tvReleaseDate;
    @BindView(R.id.btn_delete_userinfo)
    Button btnDeleteUserinfo;
    private SPUtils spUtils;

    public SettingFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_setting, container, false);
        unbinder = ButterKnife.bind(this, inflate);

        spUtils = new SPUtils(getContext().getPackageName());

        initData();


        return inflate;
    }


    public void initData() {
        tvReleaseDate.setText(getResources().getString(R.string.param_update_date));
        tvVersion.setText(getResources().getString(R.string.param_version_desc));
        tvImgPath.setText(spUtils.getString("tvImgPath", getResources().getString(R.string.param_pic_path)));
        tvServerPath.setText(spUtils.getString( "tvServerPath", getResources().getString(R.string.param_server_path)));
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnFocusChange(R.id.tv_server_path)
    public void onFocusChange(View v, boolean hasFocus) {
        if (!hasFocus) {
            spUtils.putString( "tvServerPath", tvServerPath.getText().toString());
        }
    }

    @OnClick({R.id.tv_pic_delete, R.id.btn_delete_userinfo, R.id.tv_img_path, R.id.tv_server_path})
    public void onViewClicked(View v) {

        switch (v.getId()) {
            case R.id.tv_pic_delete:
                //TODO 历史图片已清除
                ToastUtils.showShortToast("历史图片已清除");
                break;
            case R.id.btn_delete_userinfo:
                //TODO 用户信息已清除
                ToastUtils.showShortToast( "用户信息已清除");
                break;
            case R.id.tv_img_path:
//                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
//                intent.setType("*/*");//设置类型，我这里是任意类型，任意后缀的可以这样写。
//                intent.addCategory(Intent.CATEGORY_OPENABLE);
//                startActivityForResult(intent,2);

                Intent intent = new Intent(getContext(), FileManageActivity.class);
                startActivityForResult(intent, 2);
                break;
            case R.id.tv_server_path:
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2 && resultCode == 2) {
            Bundle bundle = null;
            if (data != null && (bundle = data.getExtras()) != null) {
                String file = bundle.getString("file");
                spUtils.putString("tvImgPath", file);
                tvImgPath.setText(file);
            }
        }
    }
}
